<?php

class Service_Base implements Service
{
	
	/**
	 *
	 * @var Db_Adapter
	 */
	protected $adapter;
	/**
	 * @return Db_Adapter
	 */
	public function getAdapter() {
		if (!$this->adapter) {
			$this->adapter = Db::getConnection();
		}
		return $this->adapter;
	}

	
	/**
	 * Adding limit constraints from specified Criteria object to select
	 * 
	 * @param Db_Select $select
	 * @param Criteria $criteria
	 * @return Db_Select
	 */
	public static function addLimitConstraint(Db_Select $select, Criteria $criteria)
	{
		$limit = $criteria->getLimit();
		$page = $criteria->getPage();
		if ($limit && $page) {
			$select->limitPage($limit, $page);
		} elseif ($limit) {
			$select->limit($limit);
		}
		return $select;
	}
	
	
	/**
	 * Adding order constraints from specified Criteria object to select
	 * 
	 * @param Db_Select $select
	 * @param Criteria $criteria
	 * @return Db_Select
	 */
	public static function addOrderConstraint(Db_Select $select, Criteria $criteria)
	{
		$order = $criteria->getOrder();
		$orderType = $criteria->getOrderType();
		if ($order) {
			$select->order("$order $orderType");
		}
		return $select;
	}
	
}